﻿using System;
using System.Linq;
using HVACWebService.DataAccese.Entity;

namespace HVACWebService.DataAccese.DataService
{
    public class WeChatTokenDataAccess
    {
        /// <summary>
        /// 更新Token
        /// </summary>
        /// <param name="key"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public int UpdateToken(string key,string token)
        {

            var hvacDatabaseEntities = new HvacDatabaseEntities();

            var weChatToken = hvacDatabaseEntities.WeChatToken.FirstOrDefault(wc => wc.TokenKey == key);
            if (weChatToken ==null)
            {
                weChatToken = new WeChatToken
                {
                    TokenKey = key,
                    TokenValue = token,
                    LastUpDate = DateTime.Now
                };
                hvacDatabaseEntities.WeChatToken.Add(weChatToken);
            }
            else
            {
                weChatToken.TokenValue = token;
                weChatToken.LastUpDate=DateTime.Now;
            }
            return hvacDatabaseEntities.SaveChanges();
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public WeChatToken GeWeChatToken(string key)
        {
            var hvacDatabaseEntities = new HvacDatabaseEntities();
            return hvacDatabaseEntities.WeChatToken.FirstOrDefault(wc => wc.TokenKey == key);
        }
    }
}